// pages/user/user-main/main.js
import { urlEncode } from '../../../utils/util.js'
import { Target, HttpUtil } from '../../../utils/http_util.js'
var config = require('../../../utils/config.js')
var utils = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrlBase: config.ictx,

    userInfo:'',
    loginFlag: 0, //0未登录 1以登录 
    userType:'0',
    state:0,  // 0不在线 1在线 （技师用）  

    txsrc:"/images/default_tx.png",
    imageUrl:'',

    locationInfo:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'userType',
      success(typeinfo) {
        wx.getStorage({
          key: 'userInfo',
          success(userres) {
            console.log(userres);
            that.setData({
              userInfo: userres.data,
              userType:typeinfo.data,
              loginFlag: 1
            });
            if (typeinfo.data==0){
              if (userres.data.consumerPict == ''){
                that.setData({
                  imageUrl: '/images/default_tx.png'
                });  
              }else{
                that.setData({
                  imageUrl: config.ictx + userres.data.consumerPict
                });
              }
            }
            if (typeinfo.data == 1) {
              if (userres.data.imgUrl == ''){
                that.setData({
                  imageUrl: '/images/default_tx.png'
                });  
              }else{
                that.setData({
                  imageUrl: config.ictx + userres.data.imgUrl
                });
              }
              
              if (userres.data.latitude == '' || userres.data.longitude==''){
                wx.getStorage({
                  key: 'locationInfo',
                  success(locationInfo) {
                    that.setData({
                      locationInfo: locationInfo.data.addr
                    });
                    var upAddrParam ={
                      id: userres.data.id,
                      serviceArea: locationInfo.data.addr,
                      latitude: locationInfo.data.latitude,
                      longitude: locationInfo.data.longitude
                    }
                    HttpUtil.get(Target.self, "artificerMeans/update", upAddrParam).then(res => {
                      console.log("技师地址已更新！")
                    })
                  }
                })
              }else{
                that.setData({
                  locationInfo: userres.data.serviceArea
                });
              }


            }
          },
          fail(res) {
            that.setData({
              loginFlag: 0
            });
          }
        })
      },
      fail(typeinfo) {
        that.setData({
          loginFlag: 0
        });
      }
    })
    

  },
  login:function(){
    wx.navigateTo({
      url: '/pages/user/registered/registered'
    })
  },
  collection:function(){
    var that = this;
    if(that.data.loginFlag==0){
      wx.showToast({
        title: '请先登录，再查看我的收藏',
        icon: 'none',
        duration: 2000
      })
    }else{
      wx.navigateTo({
        url: '/pages/user/collection/collection?userId='+that.data.userInfo.id,
      })
    }
  },
  message:function(){
    var that = this;
    if (that.data.loginFlag == 0) {
      wx.showToast({
        title: '请先登录，再查看我的消息',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.navigateTo({
        url: '/pages/user/message/message?userId=' + that.data.userInfo.id,
      })
    }
  },
  goBaseInfo:function(){
    var that = this;
    if (that.data.loginFlag == 0) {
      wx.showToast({
        title: '请先登录，再查看我的信息',
        icon: 'none',
        duration: 2000
      })
    } else {
      if (that.data.userType==0){  //客户
        wx.navigateTo({
          url: '/pages/user/customer/customer',
        })
      }else{
        wx.navigateTo({
          url: '/pages/user/artificer/artificer'
        })
      }
      
    }
  },
  setting:function(){
    var that = this;
    if (that.data.loginFlag == 0) {
      wx.showToast({
        title: '请先登录，再设置个人信息',
        icon: 'none',
        duration: 2000
      })
    } else {
      if (that.data.userType == 0) {  //客户
        wx.navigateTo({
          url: '/pages/user/setmsg/setmsg',
        })
      } else {
        wx.navigateTo({
          url: '/pages/user/setmsg/setmsg',
        })
      }

    }
  },
  onlinechange:function(){
    var that = this;
    var data= { id: that.data.userInfo.id}
    if (that.data.userInfo.isOnline!=1){
      
      var state = that.data.userInfo.state
      if(state == 0){
        wx.showToast({
          title: '请先完善个人资料！',
          icon: 'none',
          duration: 3000
        })
        return false;
      }else if(state== 1){
        wx.showToast({
          title: '资料审核中！',
          icon: 'none',
          duration: 3000
        })
        return false;
      } else if (state==2){
        wx.showToast({
          title: '请在服务管理添加服务！',
          icon: 'none',
          duration: 3000
        })
        return false;
      }
      data.isOnline = 1;
    }else{
      data.isOnline = 0;
    }
    HttpUtil.get(Target.self, "artificerMeans/update", data).then(res => {
      if (res.status == '0000') {
        if (that.data.userInfo.isOnline != 1) {
          wx.showToast({
            title: '已在线！',
            icon: 'none',
            duration: 2000
          })
        }else{
          wx.showToast({
            title: '已离线！',
            icon: 'none',
            duration: 2000
          })
        }
      } else {
        wx.showToast({
          title: '系统异常',
          icon: 'none',
          duration: 2000
        })
      }
      that.setData({
        "userInfo.isOnline": data.isOnline
      })
      var userinfo  = that.data.userInfo
      userinfo.isOnline = data.isOnline;
      wx.setStorageSync('userInfo', userinfo)
    })
  },
  services:function(){
    var that = this;
    var state = that.data.userInfo.state
    if (state == 0) {
      wx.showToast({
        title: '请先完善个人资料！',
        icon: 'none',
        duration: 3000
      })
      return false;
    } else if (state == 1) {
      wx.showToast({
        title: '资料审核完成才能添加项目！',
        icon: 'none',
        duration: 3000
      })
      return false;
    }else{
      wx.navigateTo({
        url: '/pages/user/services/service?artId=' + that.data.userInfo.id,
      })
    }
  },
  chooseAddress: function (e) {
    wx.navigateTo({
      url: "/pages/user/mapReload/map?url=/pages/user/user-main/main",
    })
  }
})